package org.g4studio.common.util;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * spring mvc 将表单数据封装到相应的集合中
 * @author tengyong
 *
 */
public class ParamUtil {
	
	public static HashMap<String, Object> paramMap(HttpServletRequest request){
		HashMap<String, Object> param = new HashMap<String, Object>();
		Map map = request.getParameterMap();
		Iterator keyIterator = (Iterator) map.keySet().iterator();
		while (keyIterator.hasNext()) {
			String key = (String) keyIterator.next();
			String[] value = ((String[]) (map.get(key)));
			if(value.length > 1){
				param.put(key, ParamUtil.split(value));
			}else{
				param.put(key, value[0]);
			}
		}
		return param;
	}
	
	public static String split(Object[] arr){
		StringBuffer buf = new StringBuffer();
		for(Object o : arr){
			buf.append(o).append(",");
		}
		return buf.deleteCharAt(buf.length() - 1).toString();
	}
}
